package com.wd.mymine.activity

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.TypedValue
import android.view.Gravity
import android.widget.TextView
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import com.wd.live.base.BaseActivity
import com.wd.mymine.R
import com.wd.mymine.databinding.ActivityMyGiftBinding
import com.wd.mymine.viewmodel.MyViewModel

class MyGiftActivity : BaseActivity<MyViewModel,ActivityMyGiftBinding>(R.layout.activity_my_gift) {
    override fun dataObserver() {

    }

    override fun initViews() {
        //=========================点击返回上一个页面
        binding.backGift.setOnClickListener{
            finish()
        }
        //=========================设置TabLayout
        val titles = arrayOf("我收到的","我送出的")
        for (s in titles){
            binding.tbGift.addTab(binding.tbGift.newTab().setText(s))
        }

        //=======================默认选中Tab设置文字大小
        binding.tbGift.getTabAt(0)?.let { setTab(it,true) }

        //=======================设置监听，切换时调用改变文字大小的方法
        binding.tbGift.addOnTabSelectedListener(object : OnTabSelectedListener{
            override fun onTabSelected(tab: TabLayout.Tab) {
                setTab(tab,tab.isSelected)
            }

            override fun onTabUnselected(tab: TabLayout.Tab) {
                setTab(tab,tab.isSelected)
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }


        })

    }

    //=========================选中切换Tab文字的大小
    fun setTab(tab: TabLayout.Tab, isSelected:Boolean){
        if (isSelected){
            val textView= TextView(this)
            tabText(textView,6,tab)
        }else{
            val textView= TextView(this)
            tabText(textView,5,tab)
        }
    }


    //==========================tab文字方法
    fun tabText(textView: TextView, size:Int, tab: TabLayout.Tab){
        val selectedSize=
            TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,size.toFloat(),resources.displayMetrics)
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,selectedSize)
        //居中
        textView.gravity= Gravity.CENTER
        //文字颜色
        textView.setTextColor(Color.BLACK)
        //把tab文字赋值给textView
        textView.text=tab!!.text
        tab.customView=textView
    }



}